package com.hibase.core.rule.web.ruletableversion;

import com.github.pagehelper.PageInfo;
import com.hibase.common.entity.ResponseModel;
import com.hibase.common.util.EntityUtil;
import com.hibase.core.rule.entity.ruletable.query.TableQuerySearchVO;
import com.hibase.core.rule.entity.ruletable.vo.RuleTableFieldListVO;
import com.hibase.core.rule.entity.ruletable.vo.RuleTableFieldSearchVO;
import com.hibase.core.rule.entity.ruletable.vo.RuleTableViewVO;
import com.hibase.core.rule.entity.ruletableversion.RuleTableVersion;
import com.hibase.core.rule.entity.ruletableversion.vo.*;
import com.hibase.core.rule.service.ruletable.RuleTableFieldService;
import com.hibase.core.rule.service.ruletable.RuleTableQueryService;
import com.hibase.core.rule.service.ruletableversion.RuleTableVersionService;
import com.hibase.core.web.annotation.HibaseGetRequestMapping;
import com.hibase.core.web.annotation.HibasePostRequestMapping;
import com.hibase.core.web.entity.OperationIdEntity;
import com.hibase.core.web.entity.OperationIdsEntity;
import com.hibase.common.exception.HibaseAssert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;

/**
 * <p>
 * 动态规则version表 前端控制器
 * </p>
 *
 * @author zjw
 * @since 2020-10-27
 */
@RestController
@RequestMapping("/rule-table-version")
@Api(tags = "动态规则version表")
public class RuleTableVersionController{

    @Autowired
    private RuleTableVersionService ruleTableVersionService;

    @Autowired
    private RuleTableQueryService ruleTableQueryService;

    @Autowired
    private RuleTableFieldService ruleTableFieldService;

    @ApiOperation(value = "查询动态规则version表列表", httpMethod = "POST")
    @HibasePostRequestMapping(value = "selectRuleTableVersionList")
    public ResponseModel<RuleTableVersionListVO> selectRuleTableVersionList(@RequestBody RuleTableVersionSearchVO searchVO) {

        PageInfo result = ruleTableVersionService.selectRuleTableVersionList(searchVO);

        result.setList(EntityUtil.transform(result.getList(), RuleTableVersionListVO.class));

        return ResponseModel.successMsg(result);
    }
    @ApiOperation(value = "查询动态规则version表列表", httpMethod = "POST")
    @HibasePostRequestMapping(value = "selectTableList")
    public ResponseModel<RuleTableVersionListVO> selectTableData(@RequestBody RuleTableVersionSearchVO searchVO) {

        PageInfo result = ruleTableVersionService.selectRuleTableVersionList(searchVO);

        result.setList(EntityUtil.transform(result.getList(), RuleTableVersionListVO.class));

        return ResponseModel.successMsg(result);
    }

    @ApiOperation(value = "查询动态表data", httpMethod = "POST")
    @HibasePostRequestMapping(value = "selectTable")
    public ResponseModel selectTable(@RequestBody TableQuerySearchVO searchVO){

        return ResponseModel.successMsg(ruleTableQueryService.selectTableData(searchVO));
    }


    @ApiOperation(value = "查询动态规则表详情+表字段", httpMethod = "POST")
    @HibasePostRequestMapping(value = "viewRuleTableDetails")
    public ResponseModel<RuleTableViewVO> viewRuleTableDetails(@RequestBody OperationIdEntity operationidEntity) {

        return ResponseModel.successMsg(ruleTableVersionService.viewRuleTable(operationidEntity.getId()));
    }

    @ApiOperation(value = "新增动态规则version表记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "insertRuleTableVersion")
    public ResponseModel<String> insertRuleTableVersion(@RequestBody RuleTableVersionInsertVO insertVO) {

        RuleTableVersion entity = EntityUtil.transform(insertVO, RuleTableVersion.class);

        ruleTableVersionService.saveRuleTableVersion(entity);

        return ResponseModel.successMsg(entity.getId());
    }

    @ApiOperation(value = "修改动态规则version表记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "updateRuleTableVersion")
    public ResponseModel<String> updateRuleTableVersion(@RequestBody RuleTableVersionUpdateVO updateVO) {

        RuleTableVersion entity = EntityUtil.transform(updateVO, RuleTableVersion.class);

        ruleTableVersionService.saveRuleTableVersion(entity);

        return ResponseModel.successMsg(entity.getId());
    }

    @ApiOperation(value = "查询动态规则version表详情", httpMethod = "POST")
    @HibasePostRequestMapping(value = "viewRuleTableVersionDetails")
    public ResponseModel<RuleTableVersionViewVO> viewRuleTableVersionDetails(@RequestBody OperationIdEntity operationidEntity) {

        return ResponseModel.successMsg(EntityUtil.transform(ruleTableVersionService.viewRuleTableVersion(operationidEntity.getId()), RuleTableVersionViewVO.class));
    }

    @ApiOperation(value = "删除动态规则version表记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "/deleteRuleTableVersion")
    public ResponseModel<String> deleteRuleTableVersion(@RequestBody OperationIdsEntity delete) {

        ruleTableVersionService.deleteRuleTableVersion(delete.getIds());

        return ResponseModel.successMsg();
    }
    @ApiOperation(value = "导入动态规则version表记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "/importRuleTableVersion")
    public ResponseModel<String> importRuleTableVersion(@RequestParam("file") MultipartFile multipartFile, @RequestParam("id") String id) {

        InputStream inputStream = null;

        try {

            inputStream = multipartFile.getInputStream();

        } catch (Exception e) {

            return ResponseModel.failMsg(2, "上传文件失败");
        }
        HibaseAssert.notNull(inputStream, "请上传文件");

        //HibaseAssert.isTrue(multipartFile.getOriginalFilename().endsWith("xlsx"), "请使用标准excel模板!");

        //String originalFilename = multipartFile.getOriginalFilename();

        ruleTableVersionService.importRuletableVersion(inputStream,id);

        return ResponseModel.successMsg();
    }
    @ApiOperation(value = "导出动态规则version表模板", httpMethod = "GET")
    @HibaseGetRequestMapping(value = "/exportRuleTableVersion")
    public Object exportRuleTableVersion(OperationIdEntity operationIdEntity, HttpServletResponse response) {

        ruleTableVersionService.exportRuleTableVersion(operationIdEntity.getId(),response);

        return null;
    }

    @ApiOperation(value = "导出动态规则version表数据", httpMethod = "GET")
    @HibaseGetRequestMapping(value = "/exportRuleTableVersionData")
    public Object exportRuleTableVersionData(OperationIdEntity operationIdEntity, HttpServletResponse response) {

        ruleTableVersionService.exportRuleTableVersionData(operationIdEntity.getId(),response);

        return null;
    }

    @ApiOperation(value = "查询动态规则字段表列表", httpMethod = "POST")
    @HibasePostRequestMapping(value = "selectRuleTableFieldList")
    public ResponseModel<RuleTableFieldListVO> selectRuleTableFieldList(@RequestBody RuleTableFieldSearchVO searchVO) {

        PageInfo result = ruleTableVersionService.selectRuleTableFieldList(searchVO);

        result.setList(EntityUtil.transform(result.getList(), RuleTableFieldListVO.class));

        return ResponseModel.successMsg(result);
    }

    @ApiOperation(value = "上线", httpMethod = "POST")
    @HibasePostRequestMapping(value = "/upLine")
    public ResponseModel<String> upLine(@RequestBody OperationIdsEntity enable) {

        ruleTableVersionService.upLine(enable.getIds());
        return ResponseModel.successMsg();
    }
    @ApiOperation(value = "下线", httpMethod = "POST")
    @HibasePostRequestMapping(value = "/downLine")
    public ResponseModel<String> downLine(@RequestBody OperationIdsEntity disable) {

        ruleTableVersionService.downLine(disable.getIds());
        return ResponseModel.successMsg();
    }
    @ApiOperation(value = "清空表数据", httpMethod = "POST")
    @HibasePostRequestMapping(value = "/deleteTableData")
    public ResponseModel<String> deleteTableData(@RequestBody OperationIdEntity operationIdEntity) {

        ruleTableVersionService.deleteTableData(operationIdEntity.getId());
        return ResponseModel.successMsg();
    }
}
